<?php

namespace App\Http\Requests\Api;

use App\Http\Requests\PastHistoryStoreRequest;
use App\Models\Patient;
use Illuminate\Database\Query\Builder;
use Illuminate\Validation\Rule;

class FillFirstVisitRequest extends PastHistoryStoreRequest
{
    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        $rules = parent::rules();

        array_pop($rules['record_id']);
        $rules['record_id'][] = Rule::exists('st_medical_records', 'id')->where(function (Builder $query) {
            $query->whereNull('deleted_at');
            $user = \Auth::user();
            if ($user instanceof Patient) {
                $query->where(['patient_id' => $user->id]);
            } else if (!$user->isDoctorsAdmin() && !$user->isAdmin()) {
                $query->where(['doctor_id' => $user->id]);
            }
        });

        $append = [
            'symptom'           => 'array|max:5',
            'symptom.*.name'    => [
                'array', 'max:5',
                Rule::exists('tn_dic_complain', 'name')->where(function (Builder $query) {
                    $query->where(['isdel' => 0]);
                }),
            ],
            'symptoms.*.name.*' => 'string',

            'symptom.*.started_at'     => 'nullable|date',
            'symptom.*.exacerbated_at' => 'nullable|date|after_or_equal:complaint.*.started_at',

            'medication'                              => 'nullable|array',
            'medication.*.name'                       => [
                'required',
                Rule::exists('st_drug_dictionary', 'name')->where(function (Builder $query) {
                    $query->whereNull('deleted_at');
                }),
            ],
            'medication.*.dosage.type'                => 'in:' . implode(',', $this->drug['dosage'][2]['type']),
            'medication.*.dosage.value'               => 'numeric|min:0|max:999|decimal:2',
            'medication.*.dosage.values'              => 'array|min:1|max:4',
            'medication.*.dosage.values.morning'      => 'numeric|decimal:2|min:0|max:999',
            'medication.*.dosage.values.atnoon'       => 'numeric|decimal:2|min:0|max:999',
            'medication.*.dosage.values.evening'      => 'numeric|decimal:2|min:0|max:999',
            'medication.*.dosage.values.before_sleep' => 'numeric|decimal:2|min:0|max:999',
            'medication.*.dosage.unit'                => 'in:' . implode(',', $this->drug['dosage'][2]['unit']),
            'medication.*.start_time'                 => 'date',
            'medication.*.end_time'                   => 'date|after_or_equal:medication.*.start_time',
            'medication.*.usage'                      => 'in:' . implode(',', $this->drug['usage'][2]),
            'medication.*.frequency'                  => 'in:' . implode(',', $this->drug['frequency'][2]),

            'cn_medication.*.drugs.*.name'   => [
                'string',
                Rule::exists('st_drug_dictionary', 'name')->where(function (Builder $query) {
                    $query->whereNull('deleted_at');
                }),
            ],
            'cn_medication.*.drugs.*.weight' => 'required|numeric|min:0|max:999',
            'cn_medication.*.usage'          => 'required|in:' . implode(',', $this->drug['usage'][1]),
            'cn_medication.*.dosage'         => 'required|in:' . implode(',', $this->drug['dosage'][1]['type']),
            'cn_medication.*.frequency'      => 'required|in:' . implode(',', $this->drug['frequency'][1]),
            'cn_medication.*.dose'           => 'required|integer|min:0|max:999',
        ];

        return array_merge($rules, $append);
    }

    public function messages()
    {
        $message = parent::messages();

        $append = [
            'symptom.*.name.array'           => '主诉症状 格式错误。',
            'symptom.*.name.max'             => '主诉症状 最多添加 :max个。',
            'symptom.*.name.exists'          => '主诉症状 添加了不存在的选项。',
            'symptom.*.symptom.array'        => '主诉症状 格式错误。',
            'symptom.*.started_at.date'      => '发病时间 格式错误。',
            'symptom.*.exacerbated_at.date'  => '加重时间 格式错误。',
            'symptom.*.exacerbated_at.after' => '加重时间 必须要晚于 发病时间。',
        ];

        return array_merge($message, $append);
    }
}
